<?php

if(!defined('API_Key')){die('Direct access not premitted');

/**
* SourceBans 1.x.x API
* 
* @version 1.0.2
* @Copyright (C) 2012 Jason Bourne
* @ All rights reserved
**/

require_once("sb_validation.php");

class sb_bans {
/**
 * @desc			Connects to the sourcebans database
 * @return			NULL if connection problem || check $this->dbcon
 * @author			Jason Bourne
 * @since			1.0.0
 */
	public function __construct() // this is executed as soon as a SBF object is initialised
	{
		$this->valid = new validate();
		$this->clear_user();
		include_once 'sb_database.php'; 
		$dsn = 'mysql:dbname='.$sb_db.';host='.$sb_host;
		
		/*Try connecting to the DB*/
		try {
			$dbh = new PDO($dsn, $sb_user, $sb_pass);
		} catch(PDOException $e) {
			echo $e->getMessage();
			$this->dbcon = NULL;
			return NULL;
		}

		/*Prepare the queries like a boss*/
		$this->check_ban = $dbh->prepare("SELECT authid FROM sb_bans WHERE authid=:steamid AND RemoveType=NULL");
		$this->dbcon = 1;
		return true;
	}	
	
/**
 * @desc			Loads the object with the user's details
 * @param	int	    $id ID number of the user
 * @return			ID number || NULL if ERROR
 * @author			Jason Bourne
 * @since			1.0.0
 */
	public function is_banned($steamid)
	{	
		if ($this->dbcon){
			if ($this->valid->steam($steamid)){
				$this->check_ban->bindParam(':steamid', $steamid);
				$this->check_ban->execute();
				$row = $this->check_ban->rowCount();
				if ($row == 0){
					// USER IS NOT BANNED
					return false;
				}elseif ($row > 0) {
					// USER IS BANNED
					return true;
				}
				// $row must be >= 0 
				return NULL;
			}
			//INVALID STEAM ID
			return NULL;
		}
		//ERROR DATABASE FAILURE
		return NULL;
	}
}
?>
